package com.lx.nba.handle;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lx.nba.entiy.User;

@Controller
@RequestMapping("user")
public class UsersHandle {
	
	private static List<User> topUsers = new ArrayList<>() ;
	
	private static List<User> belowUsers = new ArrayList<>() ;
	
	@RequestMapping(value="top/get" ,method= RequestMethod.GET)
	public @ResponseBody Map getTop(){
		
		Map<String, List<User>> map = new HashMap<>() ;
		
		map.put("data", topUsers) ;
		
		return map;
		
	}
	
	@RequestMapping(value="below/get" ,method= RequestMethod.GET)
	public @ResponseBody Map getBelow(){
		
		Map<String, List<User>> map = new HashMap<>() ;
		
		map.put("data", belowUsers) ;
		
		return map;
		
	}
	
	
	@RequestMapping(value="top/add" ,method= RequestMethod.POST)
	public @ResponseBody Map addTop(@RequestBody User user){
		
		user.setId(topUsers.size());
		
		topUsers.add(user) ;
 		
		Map<String, Boolean> map = new HashMap<>() ;
			
		map.put("status", true) ;
		
		return map;
		
	}

	@RequestMapping(value="below/add" ,method= RequestMethod.POST)
	public @ResponseBody Map addBelow(@RequestBody User user){		
		
		user.setId(topUsers.size());
		
		belowUsers.add(user) ;
 		
		Map<String, Boolean> map = new HashMap<>() ;
			
		map.put("status", true) ;
		
		return map;		
	}
	
	@RequestMapping(value="top/delete" ,method= RequestMethod.POST)
	public @ResponseBody Map deleteTop(@RequestBody User user){
		
		for( int i = 0 ; i < topUsers.size() ; i++){
			if( topUsers.get(i).getId() == user.getId()){
				topUsers.remove(i) ;
				break ;
			}
		}
 		
		Map<String, Boolean> map = new HashMap<>() ;
			
		map.put("status", true) ;
		
		return map;
		
	}
	
	@RequestMapping(value="below/delete" ,method= RequestMethod.POST)
	public @ResponseBody Map deleteBelow(@RequestBody User user){
		
		for( int i = 0 ; i < belowUsers.size() ; i++){
			if( belowUsers.get(i).getId() == user.getId()){
				belowUsers.remove(i) ;
				break ;
			}
		}
 		
		Map<String, Boolean> map = new HashMap<>() ;
			
		map.put("status", true) ;
		
		return map;
		
	}
	
	@RequestMapping(value="top/update" ,method= RequestMethod.POST)
	public @ResponseBody Map updateTop(@RequestBody User user){
		
		for( int i = 0 ; i < topUsers.size() ; i++){
			if( topUsers.get(i).getId() == user.getId()){
				topUsers.remove(i) ;
				topUsers.add(user) ;
				break ;
			}
		}
 		
		Map<String, Boolean> map = new HashMap<>() ;
			
		map.put("status", true) ;
		
		return map;
		
	}
	
	@RequestMapping(value="below/update" ,method= RequestMethod.POST)
	public @ResponseBody Map updateBelow(@RequestBody User user){
		
		for( int i = 0 ; i < belowUsers.size() ; i++){
			if( belowUsers.get(i).getId() == user.getId()){
				belowUsers.remove(i) ;
				belowUsers.add(user) ;
				break ;
			}
		}
 		
		Map<String, Boolean> map = new HashMap<>() ;
			
		map.put("status", true) ;
		
		return map;
		
	}

}
